package tcts0421;

import java.util.Arrays;
// https://leetcode-cn.com/problems/sorted-merge-lcci/
public class 面试题1001合并排序的数组0421 {
    public void merge(int[] A, int m, int[] B, int n) {
        int p1 = m - 1; //有效元素最末尾的元素下标
        int p2 = n - 1; //有效元素最末尾的元素下标
        int res = m + n - 1; //A的末尾下标
        while(p1>=0 && p2>=0 && res>=0){
            if(A[p1]>B[p2]){
                A[res] = A[p1];
                p1--;
            }else{
                A[res] = B[p2];
                p2--;
            }
            res--;
        }
        if(p1<0){
            while(res>=0){
                A[res] = B[p2];
                p2--;
                res--;
            }
        }
    }

    public static void main(String[] args) {
        /*
        A = [1,2,3,0,0,0], m = 3
B = [2,5,6],       n = 3
         */
        int A[] ={1,2,3,0,0,0};
        int B[] = {2,5,6};
        new 面试题1001合并排序的数组0421().merge(A,3,B,3);
        System.out.println(Arrays.toString(A));
    }
}
